package com.gitee.liuzhi.microservice.minio.server.controller;

import com.gitee.liuzhi.framework.starter.response.apiresult.DataResult;
import com.gitee.liuzhi.microservice.minio.server.utils.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by 2022/4/24/0024
 *
 * @author LiuZhi
 * @version V1.0
 * @description: Minio Controller
 */
@Api(tags = "Minio文件处理")
@Slf4j
@RestController
@RequestMapping("/minio")
public class MinioController {

    /**
     * 上传文件
     *
     * @param file file
     * @return fileUrl
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")
    public DataResult<String> upload(@RequestParam("file") @ApiParam("文件体") MultipartFile file) {
        return DataResult.success(MinioUtil.uploadFile(file));
    }

    /**
     * 删除文件
     *
     * @param fileUrl 文件路径
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除文件")
    public DataResult<Boolean> delete(@RequestParam("fileUrl") @ApiParam("文件路径") String fileUrl) {
        MinioUtil.deleteFile(fileUrl);
        return DataResult.success(Boolean.TRUE);
    }

    /**
     * 下载文件
     *
     * @param fileUrl  图片路径
     * @param fileName 下载后图片名称
     * @param response HttpServletResponse
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载文件")
    public void download(@RequestParam(value = "fileUrl") @ApiParam("文件路径") String fileUrl,
                         @RequestParam(value = "fileName", required = false) @ApiParam("文件下载名") String fileName,
                         HttpServletResponse response) {
        MinioUtil.downloadFile(fileUrl, fileName, response);
    }


}
